import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import chardet
import folium as fm
from folium import Marker, GeoJson
from folium.plugins import MarkerCluster, HeatMap, StripePattern
import geopandas as gpd
from geopandas import GeoSeries
from shapely.geometry import Point, LineString
import branca as br
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import chardet
import pandas as pd
import numpy as np
import folium
from folium.plugins import BeautifyIcon
import branca
import folium
from folium.plugins import MarkerCluster
from folium import IFrame
import shapely
from shapely.geometry import Point
import pandas as pd
import geopandas as gpd
from geopandas.tools import sjoin
import base64
import math
import os
import numpy as np
import seaborn as sns
import unicodedata
import pysal as ps
import branca
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
import pandas as pd
import numpy as np
import os
import time
import re
import unidecode
import time
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
import re
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_10112\208844336.py in <module>
4 import matplotlib.pyplot as plt
5 import chardet
----> 6 import folium as fm
7 from folium import Marker, GeoJson
8 from folium.plugins import MarkerCluster, HeatMap, StripePattern
ModuleNotFoundError: No module named 'folium'
1. Intersecting Institutes points with Shapefiles#
inst = pd.read_csv( r'../_data/institutos1.csv' )
inst_geo = gpd.GeoDataFrame( inst, crs = "EPSG:4326",
geometry = gpd.points_from_xy( inst.nlong_ie,
inst.nlat_ie ) )
shp_dpt = gpd.read_file( r"..\_data\INEI_LIMITE_DEPARTAMENTAL\INEI_LIMITE_DEPARTAMENTAL.shp" )
intersct_inst_geo = gpd.overlay( inst_geo , shp_dpt , how = 'intersection' )
# variables: meta de admision, total postulantes, total ingresantes, total matriculados
vars_sum = [ 'ltimoden_metaatencion', 'cuentadeid_postulante_procesoadm',
'sumaden_flagingresante', 'sumaden_flagmatriculado' ]
tot_dpt = intersct_inst_geo.groupby( ['CCDD'], as_index = False )[ vars_sum ].sum()
tot_dpt_shp = shp_dpt.merge( tot_dpt, on = 'CCDD' )
import json
import requests
import spectra
from matplotlib.colors import rgb2hex
Generate colors
n_categories = 4
full_palette = sns.color_palette('PiYG', n_colors=(n_categories - 1) * 2 + 1)
cmap_x = full_palette[n_categories - 1:]
sns.palplot(cmap_x)
cmap_y = list(reversed(full_palette))[n_categories - 1:]
sns.palplot(cmap_y)
Generate matrix of colors
cmap_xy = []
bivariate_palette = {}
for j in range(n_categories):
for i in range(n_categories):
x = spectra.rgb(*cmap_x[i][0:3])
y = spectra.rgb(*cmap_y[j][0:3])
if i == j and i == 0:
cmap_xy.append(x.darken(1.5).rgb)
elif i == 0:
cmap_xy.append(y.rgb)
elif j == 0:
cmap_xy.append(x.rgb)
else:
blended = x.blend(y, ratio=0.5)
if i == j:
blended = blended.saturate(7.5 * (i + 1))
else:
blended = blended.saturate(4.5 * (i + 1))
cmap_xy.append(blended.rgb)
bivariate_palette[(i, j)] = rgb2hex(cmap_xy[-1])
print(j, i)
cmap_xy = np.array(cmap_xy).reshape(n_categories, n_categories, 3)
0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
2 3
3 0
3 1
3 2
3 3
plt.imshow(cmap_xy)
<matplotlib.image.AxesImage at 0x1d5034308b0>
tot_dpt.head()
| CCDD | ltimoden_metaatencion | cuentadeid_postulante_procesoadm | sumaden_flagingresante | sumaden_flagmatriculado | ltimoden_metaatencion_qt | cuentadeid_postulante_procesoadm_qt | sumaden_flagingresante_qt | sumaden_flagmatriculado_qt | color | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 01 | 1213 | 1346 | 925 | 917 | 1 | 1 | 1 | 1 | #efe3b6 |
| 1 | 02 | 2969 | 4007 | 2535 | 2501 | 2 | 2 | 2 | 2 | #c6b268 |
| 2 | 03 | 1647 | 2997 | 1506 | 1506 | 1 | 2 | 1 | 1 | #efbeb8 |
| 3 | 04 | 3000 | 2829 | 1976 | 1941 | 2 | 2 | 2 | 2 | #c6b268 |
| 4 | 05 | 3107 | 3824 | 2406 | 2374 | 2 | 2 | 2 | 2 | #c6b268 |
# Generate quartiles for data
for vr in vars_sum:
tot_dpt[ f'{vr}_qt'] = pd.qcut(tot_dpt[ vr ], 4, [0, 1, 2, 3])
# Assign colors
tot_dpt['color'] = tot_dpt.apply( lambda x: bivariate_palette[tuple(x[['ltimoden_metaatencion_qt', 'cuentadeid_postulante_procesoadm_qt']].tolist())], axis = 1 )
# merge with data
merge1 = shp_dpt.merge(tot_dpt, on = 'CCDD' )
# Generate Json
shp_dpt_json = merge1.to_json( )
shp_dpt_json[ : 10000]
# Generate function to assign color
style_function = lambda x: {
'fillColor' : x['properties']['color'],
"fillOpacity": 1,
"weight": 0,
}
m1 = folium.Map(location = [ -12.0757538, -76.9863174 ], tiles="cartodbpositron", zoom_start=3)
folium.GeoJson(shp_dpt_json, style_function = style_function).add_to(m1)
m1
Make this Notebook Trusted to load map: File -> Trust Notebook